package crudfx.sound;
import javafx.animation.*;
/**
 * <p>Phrasw</p>
 * <p><b>Example</b>
 * <i>Play melody phrase</i></p>
 * <pre>
package testbox;

import javafx.stage.*;
import javafx.scene.*;
import crudfx.control.*;
import crudfx.interop.*;
import crudfx.sound.*;

var loop:Boolean=false;

var data:Chord[];
var bass:Integer=33;
var solo:Integer=48;

for(i in [1 .. 16])insert Chord{part:8 drums:[Drum{part:8 instrument:46 velocity:1}]} into data;
for(i in [1 .. 8]){insert Drum{part:8 instrument:35 velocity:1}into data[i*2-2].drums;
    insert Drum{part:8 instrument:38 velocity:1}into data[i*2-1].drums;}
for(i in [1 .. 3]){insert Voice{part:4 instrument:bass pitch:45 velocity:1}into data[i*4-4].voices;
    insert Voice{part:4 instrument:bass pitch:40 velocity:1}into data[i*4-2].voices;}
insert Voice{part:8 instrument:bass pitch:45 velocity:1}into data[12].voices;
insert Voice{part:8 instrument:bass pitch:40 velocity:1}into data[13].voices;
insert Voice{part:8 instrument:bass pitch:42 velocity:1}into data[14].voices;
insert Voice{part:8 instrument:bass pitch:44 velocity:1}into data[15].voices;
for(i in [1 .. 2]){
    insert Voice{part:8 instrument:solo pitch:69 velocity:1}into data[i*8-8].voices;
    insert Voice{part:8 instrument:solo pitch:73 velocity:1}into data[i*8-7].voices;
    insert Voice{part:4 instrument:solo pitch:76 velocity:1}into data[i*8-6].voices;
    insert Voice{part:8 instrument:solo pitch:69 velocity:1}into data[i*8-4].voices;
    insert Voice{part:8 instrument:solo pitch:73 velocity:1}into data[i*8-3].voices;
    insert Voice{part:8 instrument:solo pitch:76 velocity:1}into data[i*8-2].voices;
    insert Voice{part:8 instrument:solo pitch:76 velocity:1}into data[i*8-1].voices;}

var phrase:Phrase=Phrase{
    tempo:120
    onDone:function():Void{ if(loop)phrase.play(); }
    chords:data
    };

Stage{
    width:400
    height:400
    scene:Scene{
        content:[
            UINode{
              content:StandardButton{
                  text:bind if(phrase.playing)then"Stop" else "Play phrase"
                  action:function():Void{
                      if(phrase.playing){ phrase.playing=false; loop=false; }
                      else{ phrase.play(); }
                      }
                  }
              }
            ,UINode{
              translateY:50
              content:StandardButton{
                  text:bind if(loop)then "loop" else "single"
                  action:function():Void{ loop=not loop; }
                  }
              }
            ]
        }
    }
 * </pre>
 * @profile desktop
 * @author Sergey Surikov
 */
public class Phrase {
    /**
    Array of chords
    */
    public var chords:Chord[];
    /**
    Beats per minute
    */
    public var tempo:Number;
    var note:Number;
    /**
    Chord counter
    */
    public var counter:Integer;
    /**
    playing flag
    */
    public var playing:Boolean=false;
    /**
    Play
    */
    public function play():Void{
        playing=true;
        note=240*1000.0/tempo;
        counter=0;
        next();
        }
    /**
    next
    */
    public var onDone:function():Void;
    /**
    next
    */
    public function next():Void{
        if(not playing)return;
        chords[counter].play(tempo);
        counter=counter+1;
        //if(counter<sizeof chords){
            Timeline{
                keyFrames:KeyFrame{
                    time:Duration.valueOf(note/chords[counter-1].part)
                    action:function():Void{
                        if(counter<sizeof chords){
                          next();}
                        else{
                            playing=false;
                            onDone();
                            }
                        }
                    }
                }.play();
            /*}
        else{
            onDone();
            }*/
        }
    }
